<!--楼宇设备-->
<template>
  <div>
    <!--头部-->
    <basic-layout title="设备故障报修" :showAddBtn="false">
      <!--数据-->
      <div>
        <van-form class="dataStyle" ref="form">
          <van-cell-group>
            <div class="cellDataStyle">
              <div class="imgStyle2"/>
              <div class="cellTextStyle">
                <van-field
                  :readonly="true"
                  :disabled="true"
                  v-model="dataInfo.SBMC"
                  label="设备名称"
                  placeholder="请输入设备名称"
                  :border="false"
                />
              </div>
            </div>

            <div class="cellDataStyle">
              <div class="imgStyle3"/>
              <div class="cellTextStyle">
                <van-field
                  :readonly="true"
                  :disabled="true"
                  v-model="dataInfo.SBXH"
                  label="设备型号"
                  placeholder="请输入设备型号"
                  :border="false"
                />
              </div>
            </div>
            <div class="cellDataStyle">
              <div class="imgStyle1"/>
              <div class="cellTextStyle">
                <van-field
                  :readonly="true"
                  :disabled="true"
                  v-model="dataInfo.SWID"
                  label="实物ID"
                  placeholder=""
                  :border="false"
                />
              </div>
            </div>
            <div class="van-hairline--top vanHairlineTop"></div>

            <!--此处会跳转到历史巡视界面-->
            <div class="cellDataStyle">
              <div class="imgStyle4"/>
              <div class="cellTextStyle">
                <van-field
                  :readonly="true"
                  :disabled="true"
                  @click="jumpToHistory"
                  label="历史巡视"

                  right-icon="arrow"/>
              </div>
            </div>

            <div class="cellDataStyle">
              <div class="imgStyle5"/>
              <div class="cellTextStyle">
                <van-field
                  :readonly="true"
                  :disabled="true"
                  @click="jumpToRepairHistory"
                  label="维修历史"
                  right-icon="arrow"/>
              </div>
            </div>
            <div class="van-hairline--top vanHairlineTop"></div>

            <div class="cellDataStyle">
              <div class="imgStyle10"/>
              <div class="cellTextStyle">
                <van-field
                  readonly
                  :border="false"
                  :disabled="true"
                  v-model="dataInfo.GZQX"
                  label="故障类型"
                  placeholder="请选择"
                  @click="showType = true"
                  is-link/>
              </div>
            </div>
            <br/>

            <div class="remarkStyle">
              <div class="remarkUpTextStyle">
                <div class="imgStyle8"/>
                <div>&nbsp;&nbsp;&nbsp;&nbsp;故障描述</div>
              </div>
              <div class="remarkDownTextStyle">
                <van-field
                  v-model="dataInfo.BEIZ"
                  rows="2"
                  autosize
                  label=""
                  type="textarea"
                  placeholder="请输入故障描述"
                />
              </div>
            </div>

            <div class="van-hairline--top vanHairlineTop"></div>


            <div class="remarkStyle">
              <div class="remarkUpTextStyle">
                <div class="imgStyle9"/>
                <div>&nbsp;&nbsp;&nbsp;&nbsp;设备故障图</div>
              </div>
              <van-field name="uploader" label=" ">
                <template #input>
                  <uploadCom
                    ref="uploadRef"
                    :defaultFiles="defaultFiles"
                    @uploadFile="handleUpload"
                    @deleteFile="handleDelete"
                    :max-count="maxCount"
                  ></uploadCom>
                </template>
              </van-field>
              <div>
              </div>
            </div>
          </van-cell-group>
        </van-form>
        <fullLoading v-if="loading"></fullLoading>
        <!--工单状态选择 -->
        <van-action-sheet
          v-model="showType"
          cancel-text="取消"
          :actions="typeList"
          @select="valPopup"
        />
        <div class="btnBox">
          <van-button
            type="primary"
            color="#4eb3e9"
            size="large"
            @click="saveHandle"
          >保存</van-button
          >
        </div>
      </div>
    </basic-layout>
  </div>
</template>
<script>
import { getSbqxList, getUpload, saveXunjjg } from '@/api/srvAssurance/assetLabel/assetLabel'
import inspectionHistory from './inspectionHistory'
import inspectionRepairHistory from './inspectionRepairHistory'

export default {
  name: 'officeMaterialRegisterEdit',
  components: {
    inspectionHistory,
    inspectionRepairHistory
  },

  data () {
    return {
      maxCount: 1,
      fileList: {
        file: null,
      },
      defaultFiles: [],
      deleteAttachIdArr: [],
      loading: false,
      showType: false,
      formData: {},

      selectData: {
        UNIT: '',
        UNITVal: '',
      },

      dataInfo: {
        UNIT: '',
        UNITVal: '',
      },
      typeList: [],
      queryParam: {},
    }
  },

  created () {
    let data = this.$route.query.dataInfo
    this.initData()
    if (JSON.stringify(data) != '"[object Object]"') {
      console.log(data)
      this.dataInfo = data
      sessionStorage.setItem('inspectionEdit-dataInfo', JSON.stringify(data))
    } else {
      var item = sessionStorage.getItem('inspectionEdit-dataInfo')
      var dataParse = JSON.parse(item)
      this.dataInfo = dataParse
    }
  },

  methods: {
    valPopup (item) {
      this.selectData.UNIT = item.name
      this.selectData.UNITVal = item.value
      this.dataInfo.GZQX = item.name
      this.showType = false
    },

    jumpToHistory () {
      let _this = this
      this.$router.push({
        path: '/inspectionPlan/inspectionHistory',
        query: {
          dataInfo: this.dataInfo
        }
      })
    },

    jumpToRepairHistory () {
      let _this = this
      this.$router.push({
        path: '/inspectionPlan/inspectionRepairHistory',
        query: {
          dataInfo: this.dataInfo
        }
      })
    },

    // 预上传附件
    handleUpload (files) {

      this.fileList.file = files
    },
    // 已经保存的删除附件id
    handleDelete (arr) {
      this.deleteAttachIdArr = arr
    },

    async initData () {
      let resData = await getSbqxList(this.dataInfo.GZQX)
      console.log(resData)
      this.typeList = []
      if (resData.status === 0) {
        this.typeList = resData.data
      }
    },

    async saveHandle () {
      if (!this.selectData.UNITVal) {
        this.$toast('请先选择故障类型')
        return
      }
      this.formData.type = this.dataInfo.type
      this.formData.xunsid = this.dataInfo.XUNSID
      this.formData.shebid = this.dataInfo.SHEBID
      this.formData.xunsjg = '01'  //巡视结果
      this.formData.beiz = this.dataInfo.BEIZ
      this.formData.guzqx = this.selectData.UNITVal
      if (this.fileList.file != null) {
        let data = new FormData()
        data.append('file', this.fileList.file[0])
        let res = await getUpload(data)
        if (res.status == '0') {
          this.formData.fuj = res.id
          await this.save()
        }
      } else {
        await this.save()
      }

    },
    async save () {
      let res = await saveXunjjg(this.formData)
      if (res.status == 0) {
        this.$toast('报修成功')
        this.$router.back()
      }
    },
  }
}
</script>

<style scoped lang="less">
.van-button--large {
  height: 40px;
  line-height: 40px;
  width: 95%;
  margin: 20px auto;
}

/deep/ .van-dialog {
  height: 70%;
}

/deep/ .van-field__label {
  color: #646566;
}

/deep/ .van-dialog__content {
  overflow-y: scroll;
  height: 90%;
}
.btnBox {
  width: 100%;
  position: fixed;
  bottom: 0px;
  height: 40px;
  margin: 20px auto;
  // padding: 0 10px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.btnBox /deep/ .van-button--large {
}
</style>
